package GUI;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import Action.DatabaseConnection;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Config extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField tf_Server;
	private JTextField tf_Port;
	private JTextField tf_Database;
	private JTextField tf_Username;
	private JTextField tf_Password;
    String ServerName = "";
    String Port = "";
    String Username = "";
    String Password = "";
    String Database = "";
    File f = null;
    BufferedReader br = null;
    BufferedWriter bw = null;
    DatabaseConnection dbConnection = null;

	/**
	 * Create the dialog.
	 */
	public Config() {
		setTitle("Tùy chỉnh");
		setBounds(100, 100, 321, 317);
		getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Server name");
		lblNewLabel.setBounds(10, 24, 95, 14);
		getContentPane().add(lblNewLabel);
		
		tf_Server = new JTextField();
		tf_Server.setBounds(115, 21, 181, 20);
		getContentPane().add(tf_Server);
		tf_Server.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Port");
		lblNewLabel_1.setBounds(10, 68, 46, 14);
		getContentPane().add(lblNewLabel_1);
		
		tf_Port = new JTextField();
		tf_Port.setBounds(115, 65, 181, 20);
		getContentPane().add(tf_Port);
		tf_Port.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Database");
		lblNewLabel_2.setBounds(10, 206, 72, 14);
		getContentPane().add(lblNewLabel_2);
		
		tf_Database = new JTextField();
		tf_Database.setBounds(115, 203, 181, 20);
		getContentPane().add(tf_Database);
		tf_Database.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("Username");
		lblNewLabel_3.setBounds(10, 114, 72, 14);
		getContentPane().add(lblNewLabel_3);
		
		tf_Username = new JTextField();
		tf_Username.setBounds(115, 111, 181, 20);
		getContentPane().add(tf_Username);
		tf_Username.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("Password");
		lblNewLabel_4.setBounds(10, 159, 72, 14);
		getContentPane().add(lblNewLabel_4);
		
		tf_Password = new JTextField();
		tf_Password.setBounds(115, 156, 181, 20);
		getContentPane().add(tf_Password);
		tf_Password.setColumns(10);
		
		JButton btnSave = new JButton("LƯU");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btSaveActionPerformed(arg0);
			}
		});
		btnSave.setBounds(55, 244, 82, 23);
		getContentPane().add(btnSave);
		
		JButton btnReset = new JButton("LÀM LẠI");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btResetActionPerformed(arg0);
			}
		});
		btnReset.setBounds(186, 244, 89, 23);
		getContentPane().add(btnReset);
		getTextFromFile();
	}


	private void getTextFromFile()
	{
        f = new File("config.txt");
        if(!f.exists())
        {
            try {
            f.createNewFile();
            return;
            } catch (IOException ex) {
                Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        //Get Infomation config from config.txt
        try {
            br = new BufferedReader(new FileReader(f));
            String str = "";
            String[] str_split = null;
            while((str = br.readLine())!=null)
            {
                str_split = str.split(":");
            }
            ServerName = str_split[0];
            Port = str_split[1];
            Username = str_split[2];
            Password = str_split[3];
            Database = str_split[4];
            tf_Server.setText(ServerName);
            tf_Port.setText(Port);
            tf_Username.setText(Username);
            tf_Password.setText(Password);
            tf_Database.setText(Database);
            br.close();
        } catch (Exception ex) {

        }
        this.setLocationRelativeTo(this);
	}
	
    private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveActionPerformed
        // TODO add your handling code here:
        //Get text field
        ServerName = tf_Server.getText();
        Port = tf_Port.getText();
        Username = tf_Username.getText();
        Password = tf_Password.getText();
        Database = tf_Database.getText();

        //Check connect
        dbConnection = new DatabaseConnection(ServerName, Port, Username, Password, Database);
       try {
             if(dbConnection.Connect()==null)
        {
            JOptionPane.showMessageDialog(this, "Không thể kết nối vào cơ sở dữ liệu. Xin hãy kiểm tra lại","Lỗi kết nối",JOptionPane.ERROR_MESSAGE);
            return;
        }
           bw = new BufferedWriter(new FileWriter(f));
            String str = ServerName + ":" + Port + ":" + Username + ":" + Password + ":" + Database;

            bw.write(str);
            JOptionPane.showMessageDialog(null, "Hoàn tất lưu tùy chỉnh vào tệp!", "Lưu hoàn tất", JOptionPane.INFORMATION_MESSAGE);

            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //Close this window
        this.setVisible(false);
}
    //GEN-LAST:event_btSaveActionPerformed

    private void btResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btResetActionPerformed
        // TODO add your handling code here:
        int ch = JOptionPane.showConfirmDialog(null, "Bạn có muốn làm lại không?", "Cảnh báo", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if(ch == JOptionPane.YES_OPTION) {
            tf_Server.setText("");
            tf_Port.setText("");
            tf_Username.setText("");
            tf_Password.setText("");
            tf_Database.setText("");
        }
    }
}
